//
//  ScreenPreviewView.m
//  NaRiLSVMT
//
//  Created by jmsre on 14-8-10.
//  Copyright (c) 2014年 helloyouth.org. All rights reserved.
//

#import "ScreenPreviewView.h"
#import "SVGloble.h"
#import "MBProgressController.h"
@interface ScreenPreviewView()
{
    UILabel *lablist;
    UIImageView *imagelist;
    
}
@end
@implementation ScreenPreviewView
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        //启动定时器
        [[TimerDownImage sharedInstance] stratTimerDownImage];
        self.imageView = [[UIImageView alloc] initWithFrame:self.frame];
        self.imageView.image = [UIImage imageNamed:@"scan_background.png"];
        [self addSubview:self.imageView];
        [self customLoad];
    }
    return self;
}
-(void)customLoad
{
    self.listView = [[UIView alloc] initWithFrame:CGRectMake(0,0,1024,768)];
    self.listView.backgroundColor = [UIColor clearColor];
    //添加点击事件
    UITapGestureRecognizer *tapGestureRecoginzer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    [self.listView addGestureRecognizer:tapGestureRecoginzer];
    self.pageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.pageBtn.frame = Frame(14,40,100,34);
    [self.pageBtn setBackgroundImage:[UIImage imageNamed:@"scan_screen1_bac.png"] forState:UIControlStateNormal];
    [self.pageBtn addTarget:self action:@selector(screenBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    lablist = [[UILabel alloc] initWithFrame:CGRectMake(9,0,45,34)];
    lablist.font = [UIFont systemFontOfSize:18.0];
    lablist.tag = 0;
    lablist.textColor = [UIColor whiteColor];
    [self.pageBtn addSubview:lablist];
    
    imagelist = [[UIImageView alloc] initWithFrame:CGRectMake(55,11,13,10)];
    imagelist.image = [UIImage imageNamed:@"scan_uplist.png"];
    [self.pageBtn addSubview:imagelist];
    [self addSubview:self.pageBtn];
    
    self.talkBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.talkBtn.frame = Frame(910,34,32,32);
    [self.talkBtn setBackgroundImage:[UIImage imageNamed:@"forscreen_message.png"] forState:UIControlStateNormal];
    
    [self.talkBtn addTarget:self action:@selector(messBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.talkBtn];
    
    self.messBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.messBtn.frame = Frame(955,34,32,32);
    [self.messBtn setBackgroundImage:[UIImage imageNamed:@"forscreen_talk.png"] forState:UIControlStateNormal];
    [self.messBtn addTarget:self action:@selector(talkBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.messBtn];
    self.listScree = LoadFromNib(@"ScrollPhotoView");
    self.listScree.frame = Frame(14,70,Screen_Width-28,698);
    self.listScree.backgroundColor= ClearColor;
    self.listScree.delegate = self;
    [self addSubview:self.listScree];
    self.listScree.delegate = self;
    
    self.behindView = [[UIView alloc] init];
    self.behindView.frame = self.frame;
    self.behindView.backgroundColor = [UIColor blackColor];
    self.behindView.alpha = 0.3;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappro:)];
    [self.behindView addGestureRecognizer:tapGesture];
    
    self.proView = [[PromtViewController alloc] init];
    self.proView.view.frame = Frame(205,100, 617, 417);
    self.proView.delegate = self;
    self.talkflag = YES;
    self.dropListView = [[DropDownListView alloc] initWithFrame:CGRectMake(14,70,100,0) dataSource:self delegate:self Tag:@"0"];
    self.dropListView.mSuperView = self;
    self.dropListView.screenHeight = 2400;
    self.dropListView.screenWidth = 9600;
    self.dropListView.natureNum = 1;
    
    [self addSubview:self.dropListView];
    [AddNoticeObserver(NoticeKey_EventNull){
        [self.messBtn setBackgroundImage:[UIImage imageNamed:@"forscreen_talk.png"] forState:UIControlStateNormal];
        [self.aceDrawView removeFromSuperview];
        self.aceDrawView = nil;
        self.listScree.talkClickflag = NO;
        self.talkflag = YES;
    }];
    //全屏的按钮
    _allbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _allbtn.frame = CGRectMake(Screen_Width - 65,372,40,40);
    // btn.backgroundColor = [UIColor redColor];
    [_allbtn addTarget:self action:@selector(fullScreenBtn:) forControlEvents:UIControlEventTouchUpInside];
    [_allbtn setBackgroundImage:[UIImage imageNamed:@"scan_fullscreen.png"] forState:UIControlStateNormal];
    [self addSubview:_allbtn];
}
#pragma -make
#pragma -make ScreenDelegate
-(void)didCLickScreen:(NSString *)screenname
{
    CGRect s = [screenname boundingRectWithSize:CGSizeMake(150,0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:lablist.font} context:nil];
    CGRect r;
    if(s.size.width > 36){
    r = lablist.frame;
    r.size.width = s.size.width+2;
    lablist.frame = r;
    r = imagelist.frame;
    r.origin.x = lablist.frame.size.width+15;
    imagelist.frame = r;
    }
    else{
        r = lablist.frame;
        r.size.width = 45;
        lablist.frame = r;
        r = imagelist.frame;
        r.origin.x = lablist.frame.size.width+10;
        imagelist.frame = r;
    }
    r = self.pageBtn.frame;
    r.size.width = imagelist.frame.origin.x+imagelist.frame.size.width+32;
    self.pageBtn.frame = r;
    lablist.text = screenname;
    imagelist.image = [UIImage imageNamed:@"scan_uplist.png"];
}
-(void)panTalkinfo
{
    [self.messBtn setBackgroundImage:[UIImage imageNamed:@"forscreen_talk.png"] forState:UIControlStateNormal];
    [self.aceDrawView removeFromSuperview];
    self.aceDrawView = nil;
    self.listScree.talkClickflag = NO;
    self.talkflag = YES;
}
//播放清除
-(void)playTalkinfo
{
    [self.messBtn setBackgroundImage:[UIImage imageNamed:@"forscreen_talk.png"] forState:UIControlStateNormal];
    [self.aceDrawView removeFromSuperview];
    self.aceDrawView = nil;
    self.listScree.talkClickflag = NO;
    self.talkflag = YES;
}
//点击屏幕按钮 下拉
-(void)screenBtnClick:(id)sender
{
    UIButton *btn = (UIButton *)sender;
    if(self.dropListView.isOpen){
        [self.dropListView hideExtendedChooseView];
        self.dropListView.isOpen = NO;
        [self.listView removeFromSuperview];
    }
    else{
        imagelist.image = [UIImage imageNamed:@"scandownlist.png"];
        [self addSubview:self.listView];
        self.dropListView.numButton = btn.tag - SECTION_BTN_TAG_BEGIN;
        [self addSubview:self.dropListView];
        [self.dropListView sectionBtnTouch:(UIButton *)sender];
        self.dropListView.isOpen = YES;
        [self.listScree closeList];
    }
}
//交互信息
-(void)talkBtnClick:(id)sender
{
    [self bringSubviewToFront:_allbtn];
    if([(NSArray *)self.listScree.nowPlaydata[StringFromInt(self.listScree.nowScreen)] count]){
    MJCollectionViewController *collecView = self.listScree.nowPlaydata[@"collec"];
    NSArray *arrData = [NSArray arrayWithArray:(NSArray*)(self.listScree.nowPlaydata[StringFromInt(self.listScree.nowScreen)])];
    if(![@"0" isEqualToString:arrData[collecView.clickNum][@"width"]] && ![@"0" isEqualToString:arrData[collecView.clickNum][@"height"]]){
    if(!collecView.flagplay){
    if(self.talkflag){
    self.aceDrawView = [[ACEDrawingView alloc] init];
    self.aceDrawView.frame = CGRectMake(0,0,(Screen_Width-28),354);
    self.aceDrawView.backgroundColor = [UIColor clearColor];
    [self.aceDrawView setLineColor:ClearColor];
    [self.aceDrawView setLineWidth:0.0f];
    self.aceDrawView.backgroundColor = [UIColor clearColor];
    self.aceDrawView.forScreenFlag = NO;
        if(collecView.clickNum == collecView.figureNum)
            self.aceDrawView.forScreenall = YES;
        else
            self.aceDrawView.forScreenall = NO;
    NSString *strhostId = self.listScree.nowPlaydata[StringFromInt(self.listScree.nowScreen)][collecView.clickNum][@"hostId"];
    self.aceDrawView.hostIdarr = [NSMutableArray arrayWithArray:[strhostId componentsSeparatedByString:@","]];
    self.aceDrawView.doubleClickflag = NO;
    self.aceDrawView.drawingMode = DrawingModeNoneing;
    self.aceDrawView.backgroundColor = [UIColor clearColor];
    [self.aceDrawView configure];
    MJCollectionViewController *collecView = self.listScree.nowPlaydata[@"collec"];
    CGFloat f = 354.0;
    self.aceDrawView.zoomX = [arrData[collecView.clickNum][@"width"] intValue]/(Screen_Width-28);
    self.aceDrawView.zoomY = [arrData[collecView.clickNum][@"height"] intValue]/f;
    self.aceDrawView.screenWidth = [arrData[collecView.clickNum][@"width"] intValue];
    self.aceDrawView.screenHeight = [arrData[collecView.clickNum][@"height"] intValue];
    [self.listScree.showView addSubview:self.aceDrawView];
    [self.messBtn setBackgroundImage:[UIImage imageNamed:@"forscreen_talkclick.png"] forState:UIControlStateNormal];
    self.listScree.talkClickflag = YES;
    TcpClient *tcp = [TcpClient sharedInstance];
    [tcp writeString:[Common hostidtostring:[arrData[collecView.clickNum][@"hostId"] intValue] Metuod:@"GetEvents"]];
        self.talkflag = NO;
    }
    else{
        [self.messBtn setBackgroundImage:[UIImage imageNamed:@"forscreen_talk.png"] forState:UIControlStateNormal];
        [self.aceDrawView removeFromSuperview];
        self.aceDrawView = nil;
        self.listScree.talkClickflag = NO;
        self.talkflag = YES;
    }
    }
    else{
    [[MBProgressController sharedInstance] showTipsOnlyText:@"画面正在播放!" AndDelay:1.5];
    }
    }
    else{
    [[MBProgressController sharedInstance] showTipsOnlyText:@"加载画面缩略图失败!" AndDelay:1.5];
    }
    }
}
-(void)messBtnClick:(id)sender
{
  //提示信息
    if([(NSArray *)self.listScree.nowPlaydata[StringFromInt(self.listScree.nowScreen)] count]){
    MJCollectionViewController *collecView = self.listScree.nowPlaydata[@"collec"];
    NSArray *arrData = [NSArray arrayWithArray:(NSArray*)(self.listScree.nowPlaydata[StringFromInt(self.listScree.nowScreen)])];
    self.proView.screenId = self.listScree.screenId;
    if(!collecView.flagplay){
    if([arrData count]){
    [self addSubview:self.behindView];
    [self addSubview:self.proView.view];
    [self.proView showViewblock:StringFromId(arrData[collecView.clickNum][@"templateId"]) Name:arrData[collecView.clickNum][@"name"]];
    }
    [self.talkBtn setBackgroundImage:[UIImage imageNamed:@"forscreen_messageclick.png"] forState:UIControlStateNormal];
    }
    else{
        [[MBProgressController sharedInstance] showTipsOnlyText:@"画面正在播放!" AndDelay:1.5];
    }
    }
}
-(void)tappro:(id)gesture
{
    [self.proView.view removeFromSuperview];
    [self.behindView removeFromSuperview];
    [self.talkBtn setBackgroundImage:[UIImage imageNamed:@"forscreen_message.png"] forState:UIControlStateNormal];
}
#pragma -make proViewDelegate
//点击提示框隐藏
-(void)promtblock
{
    [self.behindView removeFromSuperview];
    [self.talkBtn setBackgroundImage:[UIImage imageNamed:@"forscreen_message.png"] forState:UIControlStateNormal];
}
//点击全屏调用
-(void)clickpresentViewController:(UIView *)view Controller:(UIViewController *)controller
{
    if([self.delegate respondsToSelector:@selector(presentForscreen:)]){
        [self.delegate presentForscreen:controller];
    }
}
//下拉框的 delegate
#pragma make == DropListDelegate
-(void)chooseAtSection:(NSInteger)section index:(NSInteger)index
{
    //点击事件的处理
    [self.listScree pageClick:index];
    [self.listView removeFromSuperview];
}
-(NSInteger)numberOfSections
{
    return 1;
}
-(NSInteger)numberOfRowsInSection:(NSInteger)section
{
    NSArray *tmparr = [NSArray arrayWithArray:self.listScree.screenArr];
    if([tmparr count]){
        return [tmparr count];
    }
    else{
        return 0;
    }
}
-(NSString *)titleInSection:(NSInteger)section index:(NSInteger) index
{
    NSDictionary *dic = [NSDictionary dictionaryWithDictionary:self.listScree.screenArr[index]];
    NSString *strpp = dic[@"name"];
    return strpp;
}
-(NSInteger)defaultShowSection:(NSInteger)section
{
    return 0;
}
-(void)moveViewBtn:(NSInteger)section
{}
//tap
-(void)tap:(UITapGestureRecognizer *)reconiger
{
    [self.listView removeFromSuperview];
    [self closeListView];
}
//关闭下拉框
-(void)closeListView
{
    if(self.dropListView.isOpen){
        [self.dropListView hideExtendedChooseView];
        self.dropListView.isOpen = NO;
        imagelist.image = [UIImage imageNamed:@"scan_uplist.png"];
    }
}
-(void)fullScreenBtn:(id)sender
{
    [self.listScree fullScreenBtn:sender];
}
@end
